﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VisualCardPrinter_NPPrinter
{

    public class VisualCardPrinter_NPPrinter
    {
        private int _ComPort;
        int Bandrate;
        byte[] _printer;

        int _x;
        int _y;
        int _direction;
        byte[] _fontname;
        int _fontsize;
        int com;
        //启动服务
        public static bool Port;
        public VisualCardPrinter_NPPrinter(int ComPort, int Bandrate, byte[] printer, int x, int y, int direction, byte[] fontname, int fontsize,ref int ret)
        {
            this._ComPort = ComPort;
            this.Bandrate = Bandrate;
            this._printer = printer;
            this._x = x;
            this._y = y;
            this._direction = direction;
            this._fontname = fontname;
            this._fontsize = fontsize;


            try
            {

                ret = (int)RCPClass.RCP11Class.CommOpen(_ComPort, Bandrate);
                com = ret;
                if (com != 0)
                {
                    Port = true;
                }
                else
                {
                    Port = false;
                }
               
            }
            catch (Exception e)
            {
                throw new Exception(e.ToString());
            }
        }
        //查询状态
        //查询状态
        public int StatusDemand()
        {
            return RCPClass.RCP11Class.StatusDemand(com);
        }
        
        //查询状态
        public bool IsCardInserted
        {
            get
            {
              
                int status = 0;

                status = RCPClass.RCP11Class.StatusDemand(com);
                //
                if (status == 0x02)
                {

                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        //寻卡并写卡
        public int WriteData(byte blocknum,string data)
        {
            byte[] code = Encoding.Default.GetBytes(data);
            //byte blocknum = 8;
            byte keymodel = 0x00;
            byte[] key = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };//CONTROL byte <FF 07 80 69>/<7F 07 88 69>
            int status = 0;
            status = RCPClass.RCP11Class.RfidCardRequestAndWriteData(com, blocknum, keymodel, key, code);
            return status;
        }
        //寻卡读卡
        public int ReadData(byte blocknum, ref string data)
        {
            RCPClass.CB_RfidCardRequestAndReadData callbackdatastruct;
            //byte blocknum = 8;
            byte keymodel = (byte)0x00;
            byte keyval = (byte)0xff;
            byte[] key = new byte[6];
            int ret;
            
            for (int i = 0; i < key.Length; i++)
            {
                key[i] = keyval;
            }
            ret = RCPClass.RCP11Class.RfidCardRequestAndReadData(com, blocknum, keymodel, key, out callbackdatastruct);            
            data = System.Text.Encoding.Default.GetString(callbackdatastruct.CallBackData);
            return ret;
        }

        public int selectReadNum()
        {
            RCPClass.CB_RfidCardRequestAndReadNum callbackdatastruct;
            int status = 0;
            status = RCPClass.RCP11Class.RfidCardRequestAndReadNum(com, out callbackdatastruct);
            return status;
        }

        //寻卡并返回序列号
        public string SelectCard()
        {

            int ret;
            RCPClass.CB_RfidCardRequestAndReadNum callbackdatastruct;
            int i;
            int status = 0;
            string callbackdata = "";
            byte[] bytestr = new byte[1];

            status = RCPClass.RCP11Class.RfidCardRequestAndReadNum(com, out callbackdatastruct);

            if (status == 0)
            {
                callbackdata = "";
                //RCP反馈原始信息
                for (i = 0; i < 4; i++) //将数组转换为Cstring型变量
                {
                    byte bt = callbackdatastruct.CallBackData[i]; //字符型

                    byte c = (byte)((bt >> 4) & 0x0F);

                    if ((c >= 0) && (c <= 9))
                        c = (byte)(c + 0x30);
                    else
                        c = (byte)(c + 0x37);

                    bytestr[0] = c;
                    callbackdata += System.Text.Encoding.Default.GetString(bytestr);

                    c = (byte)(bt & 0x0F);

                    if ((c >= 0) && (c <= 9))
                        c = (byte)(c + 0x30);
                    else
                        c = (byte)(c + 0x37);

                    bytestr[0] = c;
                    callbackdata += System.Text.Encoding.Default.GetString(bytestr);


                    bytestr[0] = c;
                    callbackdata += " ";


                }
            }

            return callbackdata;
        }
        //打印函数
        public void PrintText(string strText)
        {
            RCPClass.RCP11Class.PrintStr(_printer, System.Text.Encoding.Default.GetBytes(strText), _x, _y, _direction, _fontname, _fontsize);
        }
        //出卡指令
        public void EjectCard()
        {
            RCPClass.RCP11Class.RCPCardOut(com);
        }

        //弹卡指令
        public void TanCard()
        {
            RCPClass.RCP11Class.RCPCardOutputAuto(com);
        }
        //进卡
        public void insertCard()
        {
            RCPClass.RCP11Class.RCPCardInputAuto(com);
        }


        public void ResetPrinter()
        {
        }
        //关闭服务
        public void ClosePrinter(int m_hCom)
        {
            int ret = RCPClass.RCP11Class.CommClose(m_hCom);
        }
        //机器清洗指令
        public void CleanPrinter()
        {
            RCPClass.RCP11Class.RCPCleaningAuto(com);
        }

        public void CleanCard()
        {
            RCPClass.RCP11Class.RCPCardSingleErase(com);
        }

    }
}
